package com.zyl.edthresholdtraining.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyl.edthresholdtraining.common.AppConstant;
import com.zyl.edthresholdtraining.entity.UserInfo;
import com.zyl.edthresholdtraining.entity.UserTestAnswer;
import com.zyl.edthresholdtraining.entity.UserTestContent;
import com.zyl.edthresholdtraining.mapper.UserTestContentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author zhangyulu
 * @description
 * @date 2020/7/31/031
 */
@Slf4j
@Service
public class UserTestContentService extends ServiceImpl<UserTestContentMapper, UserTestContent> {


    @Autowired
    private UserTestAnswerService userTestAnswerService;
    @Autowired
    private UserTestContentService userTestContentService;


    public void makeUpTestContent(UserInfo userInfo) {
        List<Integer> list = new ArrayList<>(AppConstant.TOTAL);
        for (Integer i = 1; i <= AppConstant.TOTAL; i++) {
            list.add(i);
        }
        //尽量使得10个人不会出现太多相同图片的组合
        for (Long i = 0L; i < userInfo.getId(); i++) {
            Collections.shuffle(list);
        }
        Set<Integer> set = new HashSet<>();
        List<Integer> chooseImageList = new ArrayList<>(AppConstant.TOTAL/2);
        Random random = new Random();
        while (set.size() < AppConstant.TOTAL/2) {
            int i = random.nextInt(AppConstant.TOTAL);
            Integer value = list.get(i);
            boolean add = set.add(value);
            if (add) {
                chooseImageList.add(value);
            }
        }
        //对选出来的图片进行组合 并且随机赋值一个0-9的数字
        Iterator<Integer> iterator = set.iterator();
        while(iterator.hasNext()) {
            Integer next = iterator.next();
            list.remove(next);
        }
        componentImage(list,chooseImageList,userInfo);
    }
    /**
    *   组合图片并随机给一个0-9的数字  还有十张是blank
    * */
    private void componentImage(List<Integer> blankSet,List<Integer> chooseImageList,UserInfo userInfo) {
        List<Integer> list = new ArrayList<>();
        for (Integer index = 0; index < AppConstant.TOTAL; index++) {
            list.add(index);
        }
        for (Long i = 0L; i < userInfo.getId(); i++) {
            Collections.shuffle(list);
        }
        //名称
        StringBuilder sbName = new StringBuilder();
        //路径
        StringBuilder sbPath = new StringBuilder();
        //组装图片
        component(sbName,sbPath,list,chooseImageList,userInfo);
        componentBlankSet(sbName,sbPath,blankSet,userInfo);
        saveUserTestContent(sbName.toString(),sbPath.toString(),userInfo.getId());
    }
    private void componentBlankSet(StringBuilder sbName, StringBuilder sbPath, List<Integer> chooseImageList, UserInfo userInfo) {
        for (int i = 0; i < chooseImageList.size()-1; i++) {
            for (int j = i + 1; j < chooseImageList.size(); j++) {
                String imageUnitName = chooseImageList.get(i) + "" + chooseImageList.get(j);
                String imageUnitPath = AppConstant.IMAGE_DIR + chooseImageList.get(i) + AppConstant.IMAGE_SUFFIX + AppConstant.IMAGE_SPLIT + AppConstant.IMAGE_DIR + chooseImageList.get(j) + AppConstant.IMAGE_SUFFIX;
                sbName.append("," + imageUnitName);
                sbPath.append("," + imageUnitPath);
                //保存组合图片对应的正确数字
                saveUserTestAnswer(imageUnitName, -16, userInfo.getId());
            }
        }
    }

    private void component(StringBuilder sbName, StringBuilder sbPath, List<Integer> list, List<Integer> chooseImageList, UserInfo userInfo) {
        int indexT = 0;
        for (int i = 0; i < chooseImageList.size()-1; i++) {
            for (int j=i+1;j<chooseImageList.size(); j++) {
                String imageUnitName = chooseImageList.get(i) + "" + chooseImageList.get(j);
                String imageUnitPath = AppConstant.IMAGE_DIR+chooseImageList.get(i) + AppConstant.IMAGE_SUFFIX + AppConstant.IMAGE_SPLIT + AppConstant.IMAGE_DIR+chooseImageList.get(j)+ AppConstant.IMAGE_SUFFIX;
                if (i == chooseImageList.size()-2) {
                    sbName.append(imageUnitName);
                    sbPath.append(imageUnitPath);
                }else {
                    sbName.append(imageUnitName+",");
                    sbPath.append(imageUnitPath+",");
                }
                //保存组合图片对应的正确数字
                saveUserTestAnswer(imageUnitName,list.get(indexT),userInfo.getId());
                indexT++;
            }
        }
    }

    /**
     * 保存被试需要进行的测试图片组合信息
    * */
    private void saveUserTestContent(String userTestImageName, String userTestImage, Long userId) {
        UserTestContent userTestContent = new UserTestContent();
        userTestContent.setUserTestImageName(userTestImageName);
        userTestContent.setUserTestImage(userTestImage);
        userTestContent.setUserId(userId);
        userTestContentService.save(userTestContent);
    }

    /**
     * 保存被试测试组合和对应的数字信息
     * 为后面做判断进行服务
    * */
    private void saveUserTestAnswer(String imageUnitName, Integer value, Long userId) {
        UserTestAnswer userTestAnswer = new UserTestAnswer();
        userTestAnswer.setUserId(userId);
        userTestAnswer.setImageUnitName(imageUnitName);
        userTestAnswer.setNumberResult(value);
        userTestAnswerService.save(userTestAnswer);
    }
}
